﻿
using ShangHeng.HardwareSDK.CardReader;
using ShangHeng.HardwareSDK.Interface;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UHFReader
{
    public partial class Form1 : Form
    {

        private ICardReader cardReader;
        private bool isOpen = false;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {

            try
            {
                cardReader = new ZKUHFReaderCore(txtIpAddress.Text, Convert.ToInt32(txtPort.Text));
                isOpen = cardReader.Open();
                if (isOpen)
                {
                    timer1.Enabled = true;
                    timer1.Start();
                    lstCardNo.Items.Add("读卡器连接成功!");
                    btnOpen.Enabled = false;
                    btnClose.Enabled = true;                      
                }
                else
                {
                    lstCardNo.Items.Add("读卡器连接失败!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("打开读卡器失败:" + ex.Message);
            }            

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            isOpen = false;
            if(cardReader != null) cardReader.Close();            
            timer1.Stop();
            timer1.Enabled = false;
            btnClose.Enabled = false;
            btnOpen.Enabled = true;
            lstCardNo.Items.Add("读卡器已经关闭!");
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (isOpen && cardReader != null)
            {
                string id = cardReader.GetEpc();
                if(!string.IsNullOrEmpty(id))lstCardNo.Items.Add(id);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
